﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BikeSystem.BLL;
using BikeSystem.Models;

namespace BikeUI
{
    public partial class FormRentals : Form
    {

        private Account currentAccount = null;
        RentalManager rentalManager = new RentalManager();
        public FormRentals()
        {
            InitializeComponent();

            loadRentals();
        }

        public void CurrentAccount(Account account)
        {
            currentAccount = account;
            btnReturn.Enabled = account.Role.Equals(ConstData.ACCOUNT_ROLE_OPERATOR);
        }

        private void loadRentals() {

            IList<Rental> rentals = rentalManager.GetAllRentals();
            string fullTxt = txtFullText.Text;
            if (string.IsNullOrEmpty(fullTxt))
            {
                rentalBindingSource.DataSource = rentals.Where(p => p.IsReturn.Equals(!cbIsReturn.Checked)).ToList<Rental>();
            }else{
                fullTxt = fullTxt.Trim().ToUpper();

                rentalBindingSource.DataSource = rentals.Where(p => ((!string.IsNullOrEmpty(p.BikeNo)       && p.BikeNo.ToUpper().Contains(fullTxt)) ||
                                                                     (!string.IsNullOrEmpty(p.RentNo)       && p.RentNo.ToUpper().Contains(fullTxt)) ||
                                                                     (!string.IsNullOrEmpty(p.RenterName)   && p.RenterName.ToUpper().Contains(fullTxt)) ||
                                                                     (!string.IsNullOrEmpty(p.OperatorName) && p.OperatorName.ToUpper().Contains(fullTxt)) ||
                                                                     (!string.IsNullOrEmpty(p.NRIC)         && p.NRIC.ToUpper().Contains(fullTxt)) ||
                                                                     (!string.IsNullOrEmpty(p.Contact)      && p.Contact.ToUpper().Contains(fullTxt))) &&
                                                                     p.IsReturn.Equals(!cbIsReturn.Checked)).ToList<Rental>();
            }
        }

        private void cbIsReturn_CheckedChanged(object sender, EventArgs e)
        {

            loadRentals();
        }

        private void txtFullText_TextChanged(object sender, EventArgs e)
        {

            loadRentals();
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            Rental rental = (Rental)rentalBindingSource.Current;
            FormRental formRental = new FormRental();
            formRental.Text += " [" + rental.RentNo + "]";
            formRental.BindingSource.Add(rentalBindingSource.Current);

            formRental.Show();

        }

        private void btnReturn_Click(object sender, EventArgs e)
        {


            Rental rental = (Rental)rentalBindingSource.Current;


            BikeManager bikeManager = new BikeManager();
            Bike bike = bikeManager.GetBikeByBikeNo(rental.BikeNo);
            IList<RateOption> rateOptions = new List<RateOption>();
            rateOptions.Add(new RateOption(ConstData.OPTION_BICYCLE_RATE_TYPE_HOURLY, bike.HourlyRate));
            rateOptions.Add(new RateOption(ConstData.OPTION_BICYCLE_RATE_TYPE_DAILY, bike.DailyRate));

            Bill bill = new Bill();
            bill.BillNo = CommonUtils.genrateNextSerialNumber(ConstData.SYSTEM_NEXT_SERIALNUMBER_BILL);
            bill.OperatorName = currentAccount.UserName;
            bill.Rate = 0;
            bill.RentedTime = 0;
            bill.Amount = 0;
            bill.PaymentMethod = "Cash";

            rental.Bill = bill;


            FormBill formBill = new FormBill();
            formBill.BindingSource.Add(rental);

            formBill.RateOptions = rateOptions;
            formBill.Text += " for [" + rental.RentNo + "]";
            formBill.StartPosition = FormStartPosition.CenterParent;
            formBill.Show();

        }

    }
}
